/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.library.QuestionType;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.library.bo.AutoExtractParamBO;
import org.springblade.modules.library.entity.QuestionOptionEntity;
import org.springblade.modules.library.service.IQuestionBankService;
import org.springblade.modules.library.service.IQuestionOptionService;
import org.springblade.modules.library.vo.QuestionBankVO;
import org.springblade.modules.library.vo.QuestionOptionVO;
import org.springblade.modules.material.entity.PaperReleaseEntity;
import org.springblade.modules.material.entity.PaperReleaseRuleEntity;
import org.springblade.modules.material.excel.PaperReleaseExcel;
import org.springblade.modules.material.mapper.PaperReleaseMapper;
import org.springblade.modules.material.service.IPaperReleaseRuleService;
import org.springblade.modules.material.service.IPaperReleaseService;
import org.springblade.modules.material.service.IPaperService;
import org.springblade.modules.material.vo.PaperReleaseDetailVO;
import org.springblade.modules.material.vo.PaperReleaseVO;
import org.springblade.modules.material.wrapper.PaperReleaseWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 试卷发布表 服务实现类
 *
 * @author Kris
 * @since 2023-04-07
 */
@AllArgsConstructor
@Service
public class PaperReleaseServiceImpl extends BaseServiceImpl<PaperReleaseMapper, PaperReleaseEntity> implements IPaperReleaseService {
	private final IQuestionBankService questionBankService;
	private final IQuestionOptionService questionOptionService;
	private final IPaperReleaseRuleService paperReleaseRuleService;
	private final SnowFlakeUtil flakeUtil;
	private final IPaperService paperService;



	@Override
	public IPage<PaperReleaseVO> selectPaperReleasePage(IPage<PaperReleaseVO> page, PaperReleaseVO paperRelease) {
		return page.setRecords(baseMapper.selectPaperReleasePage(page, paperRelease));
	}

	@Override
	public IPage<PaperReleaseVO> selectPersonalPaperReleasePage(IPage<PaperReleaseVO> page, PaperReleaseVO paperRelease) {
		return  page.setRecords(baseMapper.selectPersonalPaperReleasePage(page, paperRelease));
	}

	@Override
	public IPage<PaperReleaseVO> studentExamMyself(IPage<PaperReleaseVO> page, PaperReleaseVO paperRelease) {
		return  page.setRecords(baseMapper.studentExamMyself(page, paperRelease));
	}


	@Override
	public List<PaperReleaseExcel> exportPaperRelease(Wrapper<PaperReleaseEntity> queryWrapper) {
		List<PaperReleaseExcel> paperReleaseList = baseMapper.exportPaperRelease(queryWrapper);
		//paperReleaseList.forEach(paperRelease -> {
		//	paperRelease.setTypeName(DictCache.getValue(DictEnum.YES_NO, PaperRelease.getType()));
		//});
		return paperReleaseList;
	}

	@Override
	public PaperReleaseVO getExamPaper(Long releaseId) {
		return baseMapper.getExamPaper(releaseId);
	}

	@Override
	public PaperReleaseVO getRuleExamPaper(Long releaseId) {
		PaperReleaseEntity paperReleaseEntity=this.getById(releaseId);
		PaperReleaseVO paperReleaseVO = PaperReleaseWrapper.build().entityVO(paperReleaseEntity);

		List<PaperReleaseRuleEntity> releaseRuleEntityList = paperReleaseRuleService.list(Wrappers.<PaperReleaseRuleEntity>query()
			.lambda().eq(PaperReleaseRuleEntity::getReleaseId, releaseId));

		ArrayList<PaperReleaseDetailVO> totalList=new ArrayList<>();
		List<Long> selectedQuestionIds=new ArrayList<>();//已被选择/抽中的题目ids
		releaseRuleEntityList.forEach(rule->{
			AutoExtractParamBO item=new AutoExtractParamBO();
			BeanUtil.copy(rule,item);
			item.setSelectedQuestionIds(selectedQuestionIds);
			List<QuestionBankVO> queryList = questionBankService.selectQuestionBankByParams(item);
			if (CollectionUtil.isEmpty(queryList))return;
			selectedQuestionIds.addAll(queryList.stream().map(QuestionBankVO::getId).collect(Collectors.toList()));
			//将试题转换成paperReleaseDetail
			List<PaperReleaseDetailVO> releaseDetailVOList = queryList.stream().map(queryVo -> {
				PaperReleaseDetailVO paperReleaseDetailVO = new PaperReleaseDetailVO();
				paperReleaseDetailVO.setId(flakeUtil.snowflakeId());
				paperReleaseDetailVO.setContent(queryVo.getContent());
				paperReleaseDetailVO.setQuestionType(queryVo.getQuestionType());
				paperReleaseDetailVO.setDifficultyLevel(queryVo.getDifficultyLevel());
				paperReleaseDetailVO.setQuestionId(queryVo.getId());
				paperReleaseDetailVO.setReleaseId(releaseId);
				paperReleaseDetailVO.setBankName(queryVo.getBankName());
				paperReleaseDetailVO.setQuestionProperties(queryVo.getQuestionProperties());
				paperReleaseDetailVO.setQuestionNumber(queryVo.getQuestionNumber());
				paperReleaseDetailVO.setValue(getValueByQuestionType(queryVo.getQuestionType(),paperReleaseEntity));

				//附上选项
				if (QuestionType.SHORT.getCode().intValue()==queryVo.getQuestionType() ){ //如果是简答题
					ArrayList<QuestionOptionVO>  questionOptionVOS=new ArrayList<>();
					QuestionOptionVO questionOptionVO = new QuestionOptionVO();
					questionOptionVO.setQuestionId(queryVo.getId());
					questionOptionVOS.add(questionOptionVO);
					paperReleaseDetailVO.setQuestionOptionList(questionOptionVOS);
				}else {
					paperReleaseDetailVO.setQuestionOptionList(
						//将questionOption转成questionOptionVO
						questionOptionService.list(Wrappers.<QuestionOptionEntity>query()
							.lambda().eq(QuestionOptionEntity::getQuestionBankId, queryVo.getId())).stream().map(questionOption -> {
							QuestionOptionVO questionOptionVO = new QuestionOptionVO();
							BeanUtil.copyProperties(questionOption, questionOptionVO);
							return questionOptionVO;
						}).collect(Collectors.toList())

					);
				}

				return paperReleaseDetailVO;
			}).collect(Collectors.toList());
			totalList.addAll(releaseDetailVOList);
		});
		paperReleaseVO.setPaperReleaseDetailList(paperService.sortExamPaperDetailByQuestionType(totalList));

		return paperReleaseVO;
	}
	//根据题目类型获取试题分数
	public Integer getValueByQuestionType(int questionType,PaperReleaseEntity paperReleaseEntity ){
		Integer value=0;
		switch (questionType){
			case 1:
				value=paperReleaseEntity.getSingleScore();
				break;
			case 2:
				value=paperReleaseEntity.getMultipleScore();
				break;
			case 3:
				value=paperReleaseEntity.getJudgeScore();
				break;
			case 4:
				value=paperReleaseEntity.getFillScore();
				break;
			case 5:
				value=paperReleaseEntity.getShortScore();
				break;
		}
		return value;
	}

}
